package conson.tech.manage.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import conson.tech.manage.api.app.constant.AppCommon;
import conson.tech.manage.app.entity.pojo.AppVersion;
import conson.tech.manage.app.entity.vo.AppVersionVo;
import conson.tech.manage.app.entity.vo.PageVo;
import conson.tech.manage.app.entity.vo.QueryAppVersionVo;
import conson.tech.manage.app.mapper.AppVersionMapper;
import conson.tech.manage.app.service.AppVersionService;
import conson.tech.manage.common.util.DateUtil;
import conson.tech.manage.common.util.EntityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: AppVersionServiceImpl
 * Description:
 * Author: HXL
 * Date: 2020/4/27 10:58 上午
 * Version: 1.0
 */
@Service("appVersionService")
public class AppVersionServiceImpl implements AppVersionService {
    @Autowired
    private AppVersionMapper appVersionMapper;
    @Override
    public List<QueryAppVersionVo> queryAppVersionList() {
       List<AppVersion> list = appVersionMapper.queryAppVersionList();
        return EntityUtils.listToList(list, QueryAppVersionVo.class);
    }

    @Transactional
    @Override
    public int addVersion(AppVersionVo vo) {
        AppVersion appVersion = new AppVersion();
        BeanUtils.copyProperties(vo, appVersion);
        appVersion.setCreatedTime(LocalDateTime.now());
        appVersion.setUpdatedTime(LocalDateTime.now());
        appVersion.setDelFlag(1);
        return appVersionMapper.insert(appVersion);
    }

    @Override
    @Transactional
    public int updateVersion(AppVersionVo vo) {
        AppVersion appVersion = new AppVersion();
        BeanUtils.copyProperties(vo, appVersion);
        appVersion.setUpdatedTime(LocalDateTime.now());
        return appVersionMapper.updateById(appVersion);
    }

    @Override
    public Page<AppVersionVo> queryAppVersionPage(PageVo vo) {
        Page<AppVersion> page = new Page();
        page.setCurrent(vo.getPageIndex());
        page.setSize(vo.getPageSize());
        Integer platformType;
        if (vo.getId() != null && vo.getId() == 1){
            platformType = 1;
        }else if (vo.getId() != null && vo.getId() == 2){
            platformType = 2;
        }else {
            platformType = null;
        }
        Page<AppVersion> appVersionPage = appVersionMapper.queryAppVersionPage(page,platformType);
        Page<AppVersionVo> voPage = new Page<>();
        List<AppVersionVo> appAdvertVoList = new ArrayList<>();
        if (appVersionPage != null){
            List<AppVersion> records = appVersionPage.getRecords();
            if (!CollectionUtils.isEmpty(records)){
                for (AppVersion record : records) {
                     if (record == null)continue;
                    AppVersionVo versionVo = new AppVersionVo();
                     BeanUtils.copyProperties(record, versionVo);
                     if (record.getCreatedTime() != null) {
                         versionVo.setCreatedTime(DateUtil.getLocalDateTimeStr(record.getCreatedTime()));
                     }
                     if (record.getUpdatedTime() != null) {
                         versionVo.setUpdatedTime(DateUtil.getLocalDateTimeStr(record.getUpdatedTime()));
                     }
                    appAdvertVoList.add(versionVo);
                }
            }
            BeanUtils.copyProperties(appVersionPage, voPage);
        }
        voPage.setRecords(appAdvertVoList);
        return voPage;
    }
    /**
     * Title: 检查版本
     * Description:
     * @param vo
     * @return: java.util.List<conson.tech.manage.app.entity.pojo.AppVersion>
     * Company: GX
     * Author HXL
     * Date 2020/5/26 2:36 下午
     */
    @Override
    public List<AppVersion> checkAppVersion(AppVersionVo vo) {
        AppVersion appVersion = new AppVersion();
        BeanUtils.copyProperties(vo, appVersion);
        return  appVersionMapper.checkAppVersion(appVersion);
    }

    /**
     * Title: 根据版本查询版本
     * Description:
     * @param version
     * @param platformType
     * @return: java.util.List<conson.tech.manage.app.entity.pojo.AppVersion>
     * Company: GX
     * Author HXL
     * Date 2020/5/26 2:36 下午
     */
    @Override
    public List<AppVersion> queryAppVersionByVersionNum(String version, Integer platformType) {
        AppVersion appVersion = new AppVersion();
        appVersion.setVersionNum(version);
        appVersion.setPlatformType(platformType);
        appVersion.setDelFlag(AppCommon.APP_DEL_FLAG_ONE);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(appVersion);
        return appVersionMapper.selectList(queryWrapper);
    }
    /**
     * Title: 根据版本值查询版本
     * Description:
     * @param list
     * @return: java.util.List<conson.tech.manage.app.entity.pojo.AppVersion>
     * Company: GX
     * Author HXL
     * Date 2020/5/26 2:37 下午
     */
    @Override
    public List<AppVersion> queryAppVersionByValue(List<Integer> list) {

        return appVersionMapper.queryAppVersionByValue(list);
    }
    /**
     * Title: 根据版本状态查询版本
     * Description:
     * @param versionState
     * @return: java.util.List<conson.tech.manage.app.entity.pojo.AppVersion>
     * Company: GX
     * Author HXL
     * Date 2020/5/26 3:04 下午
     */
    @Override
    public List<AppVersion> queryNewVersionByVersionState(int versionState) {
        return appVersionMapper.queryNewVersionByVersionState(versionState);
    }
    /**
     * Title: 根据版本状态和平台类型查询版本
     * Description:
     * @param versionState
     * @param platformType
     * @return: java.util.List<conson.tech.manage.app.entity.pojo.AppVersion>
     * Company: GX
     * Author HXL
     * Date 2020/6/19 4:04 下午
     */
    @Override
    public List<AppVersion> queryNewVersionData(int versionState, Integer platformType) {
        return appVersionMapper.queryNewVersionData(versionState,platformType);
    }

}
